﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using eCommerce.core;
using eCommerce.core.Authenticated;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;

namespace eCommerce.cms.Controls.Article
{
    public partial class CreateOrEdit : AuthenticatedControl
    {
        protected int ArticleId;

        protected void Page_Load(object sender, EventArgs e)
        {
            ArticleId = Utils.GetIdFromRequestString(Utils.ArticleId, 0);
            
            if (!IsPostBack)
            {
                ltTitle.Text = ArticleId > 0 ? "Sửa bài viết" : "Tạo mới bài viết";

                ListTreeCategory();

                if (ArticleId > 0)
                    BindArticle(ArticleId);
            }
        }      

        private void ListTreeCategory()
        {
            var all = new List<CategoryEntity>();
            var articles = new List<CategoryEntity>();
            var guides = new List<CategoryEntity>();

            CategoryBo.GetListTree(articles, (int)CategoryType.Article, (int)CategoryType.Level, (int)CategoryType.Root, (int)CategoryType.Active);
            CategoryBo.GetListTree(guides, (int)CategoryType.Guide, (int)CategoryType.Level, (int)CategoryType.Root, (int)CategoryType.Active);

            for (int i = 0; i < articles.Count; i++)
                all.Add(articles[i]);

            for (int i = 0; i < guides.Count; i++)
                all.Add(guides[i]);

            if (all.Count > 0)
            {
                ddlCategory.DataSource = all;
                ddlCategory.DataBind();
                ddlCategory.Items.Insert(0, new ListItem("-- Chuyên mục --", "0"));
            }
        }

        private void BindArticle(int id)
        {
            var o = ArticleBo.GetById(id);
            if (o == null) return;

            ddlCategory.SelectedValue   = o.CategoryId.ToString();
            ddlStatus.SelectedValue     = o.Status.ToString();
            txtTitle.Text               = o.Title;
            txtSapo.Text                = o.Sapo;
            txtBody.Text                = o.Body;
            txtSource.Text              = o.Source;
            txtImagePath.Text           = o.ImgAvatar;
            txtPublishOn.Text           = Utils.FormatDateVietNam(o.PublishOn);
            txtTime.Text                = Utils.FormatTimeVietNam(o.PublishOn);                            
        }

        protected void BtnSave(object sender, EventArgs e)
        {
            ArticleEntity o;
            
            var publishOn = Utils.ValidDateTime(txtPublishOn.Text, txtTime.Text);
            var status    = Utils.ToInt32(ddlStatus.SelectedValue);

            if (ArticleId > 0)
            {
                o = ArticleBo.GetById(ArticleId);
                o.CategoryId       = Utils.ToInt32(ddlCategory.SelectedValue);
                o.ArticleId        = ArticleId;
                o.Title            = txtTitle.Text;
                o.Url              = Utils.UnsignAndDash(o.Title);
                o.Sapo             = txtSapo.Text;
                o.Body             = txtBody.Text;
                o.Source           = txtSource.Text;
                o.ImgAvatar        = txtImagePath.Text;
                o.PublishOn        = publishOn;
                o.Status           = status;
                o.EditedBy         = CurrentUser.AdminUserId.ToString();
                o.ApprovedBy       = CurrentUser.AdminUserId.ToString();                
            }
            else
            {
                o = new ArticleEntity();
                o.CategoryId       = Utils.ToInt32(ddlCategory.SelectedValue);
                o.Title            = txtTitle.Text;
                o.Url              = Utils.UnsignAndDash(o.Title);
                o.Sapo             = txtSapo.Text;
                o.Body             = txtBody.Text;
                o.Source           = txtSource.Text;
                o.ImgAvatar        = txtImagePath.Text;
                o.PublishOn        = publishOn;
                o.Status           = status;
                o.CreatedBy        = CurrentUser.AdminUserId.ToString();
            }

            ArticleBo.Save(o);

            Response.Redirect(UrlHelper.ListArticle);
        }     
    }
}